﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.Runtime.Serialization;

using System.ComponentModel;

namespace MSAP.ObjectsStore
{
    [DataContract]
    public class RecordBase<TRECORD> : IRecord, 
                                       INotifyPropertyChanged
                                       where TRECORD : IRecord, new()
    {
        public RecordBase()
        {
            RecordState = eRecordState.Unchanged;
            RecordStore = eRecordStore.Memory;
        }

        [DataMember]
        public Guid Oid
        {
            get { return m_Oid; }
            set
            {
                if (m_Oid != value)
                {
                    m_Oid = value;
                    RaisePropertyChanged("Oid");
                }
            }
        }
        public eRecordState RecordState
        {
            get { return m_RecordState; }
            set
            {
                if (m_RecordState != value)
                {
                    m_RecordState = value;
                    RaisePropertyChanged("RecordState");
                }
            }
        }
        public eRecordStore RecordStore 
        { 
            get {return m_RecordStore;} 
            set 
            {
                if (m_RecordStore != value)
                {
                    m_RecordStore = value;
                    RaisePropertyChanged("RecordStore");
                }
            }
        }

        public bool IsDirty() 
        { 
            return RecordState != eRecordState.Unchanged; 
        }

        public void Save() 
        {
            // Save
            RecordState = eRecordState.Unchanged;
            RecordStore = eRecordStore.Persistent;
            RaisePropertyChanged("RecordState");
            RaisePropertyChanged("RecordStore");
        }
        public void Load()  
        {
            // Load
            RecordState = eRecordState.Unchanged;
            RecordStore = eRecordStore.Persistent;
            RaisePropertyChanged("RecordState");
            RaisePropertyChanged("RecordStore");
        }
        public void Delete()
        {
            // Delete
            RecordState = eRecordState.Deleted;
            RecordStore = eRecordStore.Memory;
            RaisePropertyChanged("RecordState");
            RaisePropertyChanged("RecordStore");
        }

        public void Lock() { m_Lock = true; }
        public void UnLock() { m_Lock = false; }
        public bool IsLock() { return m_Lock; }

        #region INotifyPropertyChanged Members
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null))
            {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));

                if (this.RecordState == eRecordState.Unchanged)
                    this.RecordState = eRecordState.Updated;
            }
        }
        #endregion

        #region Fields
        private bool m_Lock;
        private Guid m_Oid;
        private eRecordState m_RecordState;
        private eRecordStore m_RecordStore;
        #endregion
    }
}
